/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.service.fm.deployment;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;

import fr.jade.base.JadeBaseObject;
import fr.jade.reflex.api.control.LifeCycleNotificationController;
import fr.jade.reflex.util.Reflex;

public class StartTask extends JadeBaseObject implements DeploymentTask {

  protected NewComponentTask cmpTask;
  
  public StartTask(NewComponentTask cmpTask){
    assert cmpTask != null;
    this.cmpTask=cmpTask;
  }
  
  public void execute() throws DeploymentException {
    log("execute :" +toString());
    Component cmp = cmpTask.getComponent();
    try {
      LifeCycleNotificationController lcc = Reflex.getLifeCycleNotificationController(cmp);
      LifeCycleNotificationController lccDual = Reflex.getLifeCycleNotificationController(Reflex.getDualComponent(cmp));
      lcc.rawStartFcNotification();
      lccDual.rawStartFcNotification();
    } catch (NoSuchInterfaceException e) {
      throw new DeploymentException("Start Task Error: no lifecyclecontroller", e);
    } catch (IllegalLifeCycleException e) {
      throw new DeploymentException("Start Task Error: can't start component", e);
    }
  }

  public String getLevelName(){
    return "start";
  }
  
  public String toString(){
    return "StartTask["+ cmpTask.toString() +"]";
  }
  
}
